import 'dart:convert';

import 'package:freemusic/model/AbstractSong.dart';
import 'package:freemusic/model/Lyric.dart';


class QQSong extends AbstractSong {
  int albumid;
  String albummid;
  String albumname;
  int songid;
  int size128;
  int size320;
  int sizeflac;
  int sizeogg;

  @override
  List<AbstractSong> fromRemoteJosn(Map json) {
    List<QQSong> songs = List();

    final map = json;
    Map data = map['data'];
    Map song = data['song'];
    List list = song['list'];
    list.forEach((item) {
      Map singer = item['singer'][0];
      QQSong info = QQSong();
      info.hash = item['songmid'];
      info.songName = item['songname'];
      info.singerName = singer['name'];
      info.albumid = item['albumid'];
      info.songid = item['songid'];
      info.albummid = item["albummid"];
      info.albumname = item["albumname"];
      info.size128 = item['size128'];
      info.size320 = item['size320'];
      info.sizeflac = item['sizeflac'];
      info.sizeogg = item['sizeogg'];
      info.type = "mp3";
      info.platform = "qq";
      info.timeLength = Duration(seconds: item['interval']);
      songs.add(info);
    });

    return songs;
  }

  void setLyricsFromJson(String response){
    final map = jsonDecode(response);
    if(map['retcode'] == 1101){
      return ;
    }
    String lyricsd = map['lyric'];

    this.lyrics = lyricsd.replaceAll('&#58;', ":")
                        .replaceAll("&#46;", ".")
                        .replaceAll("&#10;", "\r\n")
                        .replaceAll("&#32;", " ")
                        .replaceAll("&#13;", "")
                        .replaceAll("&#64;", "@")
                        .replaceAll("&#45;", "-")
                        .replaceAll("&#40;", "(")
                        .replaceAll("&#41;", ")");
    // _fromLyricsSetTimelength(this.lyrics);
  }

  void _fromLyricsSetTimelength(String lyric){
    int index = lyric.lastIndexOf("[");
    String timelength = lyric.substring(index+1 , index+9);
    int minute = int.parse(timelength.substring(0, 2));
    int second = int.parse(timelength.substring(3, 5));
    int millis = int.parse(timelength.substring(6, 8));
    this.timeLength = Duration(minutes: minute, seconds: second, milliseconds: millis) ;
  }

  @override
  String toString() {
    return "{\"playUrl\":\"$playUrl\","+
           "\"img\":\"$img\","+
           "\"timeLength\":${this.timeLength.inMilliseconds},"+
           "\"singerName\":\"$singerName\","+
           "\"songName\":\"$songName\","+
           "\"hash\":\"$hash\","+
           "\"type\":\"$type\","+
           "\"platform\":\"$platform\","+
           "\"albummid\":\"$albummid\","+
           "\"songid\":$songid"
           "}";
  }

  @override
  Lyric stringToLyric() {
    Lyric lyric = Lyric();
    if (lyrics == null) {
      return lyric;
    }
    List<String> list = lyrics.split("\r\n");
    list.forEach((item) {
      if (!item.endsWith("]") && item.isNotEmpty) {
        lyric.slices.add(_stringToLyricSlice(item));
      }
    });
    return lyric;
  }

  LyricSlice _stringToLyricSlice(String line) {
    LyricSlice lyricSlice = LyricSlice();
    lyricSlice.slice = line.substring(10);
    int minute = int.parse(line.substring(1, 3));
    int second = int.parse(line.substring(4, 6));
    int millis = int.parse(line.substring(7, 9));

    // lyricSlice.milliseconds = minute * 60 * 1000 + second * 1000 + millis;
    lyricSlice.milliseconds = Duration(minutes: minute, seconds: second, milliseconds: millis);
    return lyricSlice;
  }

  @override
  void fromDataBase(Map map) {
    super.fromDataBase(map);
    this.songid = map['songid'];
    this.albumid = map['albumid'];
    this.albummid = map['albummid'];
    this.albumname = map['albumname'];
    this.size128 = map['size128'];
    this.size320 = map['size320'] ;
    this.sizeflac = map['sizeflac'];
    this.sizeogg = map['sizeogg'];
  }

  @override
  Map toJsonMap() {
    Map map = super.toJsonMap();
    map['albumid'] = this.albumid;
    map['albummid'] = this.albummid;
    map['albumname'] = this.albumname;
    map['songid'] = this.songid;
    map['size128'] = this.size128;
    map['size320'] = this.size320;
    map['sizeflac'] = this.sizeflac;
    map['sizeogg'] = this.sizeogg;
    return map;
  }
}
